<?php
class GalleriesController extends GallerysAppController {

	var $name = 'Galleries';
	var $helpers = array('Html', 'Form');
	var $components =  array('Images.Attachment','RequestHandler');
	var $uses = array('Galleries.Gallery','Images.Image');
	
	function beforeFilter(){
		parent::beforeFilter();
		$this->layout = 'administrador';
		
	}
	
	function index() {
		$this->Gallery->recursive = 0;
		$this->set('galleries', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Gallery.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('gallery', $this->Gallery->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Gallery->create();
			if ($this->Gallery->save($this->data)) {
				$this->Session->setFlash(__('The Gallery has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Gallery could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Gallery', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Gallery->save($this->data)) {
				$this->Session->setFlash(__('The Gallery has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Gallery could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Gallery->read(null, $id);
			$this->data['Image'] = $this->Image->explode_images($this->data['Image']);
		}
	}
	function agregar_imagen(){
		if(!empty($this->data)){
			$id = $this->data['Gallery']['id'];
			$file_path = $this->Attachment->upload($this->data['Image']['Attachment'],'Gallery');
			if($images = $this->Image->save_image('Gallery',$id,$file_path)){
				$this->Session->setFlash(__('Galeria guardada', true));
			}else{
				$this->Session->setFlash(__('Error al guardar la imagen. Intente nuevamente', true));
			}
			$this->redirect($this->referer());
		}
	}
	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Gallery', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Gallery->del($id)) {
			$this->Session->setFlash(__('Gallery deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}
	function delete_image($id){
			$this->Image->delete_img('Gallery',$id);
			$this->Attachment->delete_files($new['Image']['files'],'News');
			$this->redirect($this->referer());
	}

}
?>